<template>
  <div id="customer-departments" class="card col-12 px-0 mt-5">
    <nav aria-label="breadcrumb" role="navigation">
      <ol class="breadcrumb pb-0">
        <li class="breadcrumb-item ms-1">
          <router-link to="/h5/customers">客户</router-link>
        </li>
        <li class="breadcrumb-item active" aria-current="page">部门信息</li>

        <li class="breadcrumb-item ms-auto me-1">
          <router-link to="/h5/passengers">员工信息</router-link>
        </li>
      </ol>
    </nav>

    <template v-if="showSearchForm === false">
      <div class="card-header">
        <router-link to="/h5/customer/department" class="btn btn-success"
          >新建</router-link
        >

        <button
          type="button"
          class="btn btn-primary float-end"
          @click.stop="showSearchOption()"
        >
          查找
        </button>
      </div>

      <table class="table table-striped table-sm">
        <thead>
          <tr>
            <th>公司</th>
            <th>部门编号</th>
            <th>部门名称</th>
            <th>上级部门</th>
            <th class="d-none">备注</th>
            <th></th>
          </tr>
        </thead>
        <tbody>
          <template v-for="(info, index) in dataList">
            <tr>
              <td>{{ info.customerName }}</td>
              <td>{{ info.departmentNo }}</td>
              <td>{{ info.name }}</td>
              <td>{{ info.parentDepartmentName }}</td>
              <td class="d-none">{{ info.remark }}</td>
              <td>
                <router-link
                  :to="`/h5/customer/department?id=` + info.id"
                  class=""
                  >修改</router-link
                >
                <router-link
                  :to="`/h5/customer/department?id=` + info.id + `&copies=1`"
                  class="ms-auto"
                  >复制</router-link
                >
                <a
                  href="javascript:void(0)"
                  @click.stop="addApprover(index)"
                  class="btn btn-sm btn-outline-info small ms-3 d-none"
                  >增加审批人</a
                >
              </td>
            </tr>
          </template>
        </tbody>
      </table>
      <nav id="pagination-box" class="float-end">
        <my-pagination
          name="pagination"
          :row-count="sc.rowCount"
          :page-total="sc.pageTotal"
          :page-no="sc.pageNo"
          @next-page="nextPage"
          @prev-page="prevPage"
          @direct-page="directPage"
        ></my-pagination>
      </nav>
    </template>
    <template v-else>
      <div class="card-body">
        <form>
          <div class="row mb-2">
            <input
              type="textfield"
              class="form-control"
              placeholder="部门名称"
              v-model.trim="searchName"
            />
          </div>
          <div class="row mb-2">
            <my-select-customer
              :customerId.sync="searchCustomerId"
            ></my-select-customer>
          </div>

          <div class="row">
            <div class="col">
              <button type="button" class="btn btn-primary" @click="search()">
                确定
              </button>
            </div>
            <div class="col">
              <button
                type="button"
                class="btn btn-secondary"
                title="清空查找条件"
                @click.stop="reset()"
              >
                重置
              </button>
            </div>
          </div>
        </form>
      </div>
    </template>

    <my-customer-approver ref="customerApproverModal"></my-customer-approver>
  </div>
</template>

<script>
import {
  searchDepartments,
  searchCustomers,
  deleteCustomerApprover,
} from "@/api/customer.js";
import MyPagination from "@/components/my-pagination.vue";
import MySelectCustomer from "@/components/my-select2-customer.vue";
import MyCustomerApprover from "@/components/modal/my-modal-customer-approver.vue";

export default {
  components: {
    MyPagination,
    MySelectCustomer,
    MyCustomerApprover,
  },
  data() {
    return {
      id: 0,
      customerId: 0,
      deptId: 0,
      name: "",
      parentDeptId: 0,
      mobile: "",
      phone: "",
      linkMan: "",
      email: "",
      remark: "",

      dataList: [],
      customers: [],

      showSearchForm: false,
      sc: {
        rowCount: 0,
        pageNo: 1,
        pageSize: 15,
        pageTotal: 0,
      },
      searchName: "",
      searchCustomerId: -1,
    };
  },
  computed: {
    isDataAdmin() {
      return this.$store.getters.isDataAdmin;
    },
  },
  mounted: function () {
    console.log(this.isDataAdmin);
    // this.search()
    console.log("mounted");
    this.search();
  },
  activated: function () {
    console.log("activated");
  },
  methods: {
    showErrMsg: function (msg, msgType) {
      this.$store.dispatch("showAlertMsg", {
        errMsg: msg,
        errMsgType: msgType,
      });
    },
    showLoading: function (msg) {
      // this.$store.dispatch('showLoading', { 'loadingText': msg })
    },
    hideLoading: function () {
      // this.$store.dispatch('hideLoading')
    },
    showSearchOption: function () {
      this.showSearchForm = true;
    },
    search: function () {
      this.showSearchForm = false;
      const params = {
        "sc.pageNo": this.sc.pageNo,
        "sc.pageSize": this.sc.pageSize,
        "sc.name": this.searchName,
        "sc.customerId": this.searchCustomerId,
      };
      console.log(params);
      // this.showLoading('查询中...')
      searchDepartments(params, (jsonResult) => {
        this.dataList = jsonResult.dataList;
        this.sc = jsonResult.page;
        // this.hideLoading()
      });
    },
    reset: function () {
      this.name = "";
      this.searchName = "";
      this.searchCustomerId = -1;
      this.sc.pageNo = 1;
      this.search();
    },
    searchCustomers: function () {
      if (this.customers.length === 0) {
        searchCustomers(null, (jsonResult) => {
          this.customers = jsonResult.dataList;
        });
      }
    },
    addApprover: function (index) {
      const info = this.dataList[index];

      this.$refs.customerApproverModal
        .modal(info)
        .then((v) => {
          console.log(v);
          if (v.status === "OK") {
            this.search();
          }
        })
        .catch((ex) => {});
    },
    deleteApprover: function (id) {
      deleteCustomerApprover(id, (v) => {
        if (v.status === "OK") {
          this.showErrMsg("操作成功");
          this.search();
        } else {
          this.showErrMsg(v.errMsg, "danger");
        }
      });
    },
    showRoleType: function (val) {
      switch (val) {
        case 10:
          return "经理";
        case 20:
          return "审批人";
        default:
          return val;
      }
    },
    prevPage: function () {
      this.sc.pageNo = this.sc.pageNo - 1;
      if (this.sc.pageNo < 1) this.sc.pageNo = 1;
      this.search();
    },
    nextPage: function () {
      this.sc.pageNo = this.sc.pageNo + 1;
      this.search();
    },
    directPage: function (pageNo) {
      this.sc.pageNo = pageNo;
      this.search();
    },
  },
};
</script>